#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/environment'
require 'active_record'
puts "Get country of each pilot"
Pilot.all.each do |p|
  %x[wget http://www.4mula1.ro/history/driver/#{p.name.gsub(' ','_')} -O /tmp/tmp.html]
  country = File.open('/tmp/tmp.html').read.gsub(/\s/,'').match(/Nation<\/td>\s*<[^>]+>([^<]+)<\/td><\/tr>/)[1].capitalize
  country = 'Brazil' if country == 'Brazilia'
  country = 'Greatbritain' if country == 'Uk' or country == '1959-08-01' or country == 'British/irish'
  country = 'Switzerland' if country == 'Swiss'
  p.country = country
  p.save!
  puts "#{p.name} is from #{p.country}"
end
